﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using xiaodingmarket_application.Interfaces;
using xiaodingmarket_application.ViewModel;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Domain_core.Notificatiions;

namespace xiaodingmarket_service.Controllers
{
    //[Authorize]
    public class ManageCommodityController : ApiController
    {
        private readonly IManageService _manageService;

        public ManageCommodityController(IManageService manageService, INotificationHandler<DomainNotification> notifications, IMediatorHandler mediator) : base(notifications, mediator)
        {
            _manageService = manageService;
        }
        /// <summary>
        /// 管理员取数据（如何做到多个管理员取数据是不重复的）
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        //[Authorize(Policy = "CanReviewCommodity")]
        [Route("Management")]
        public IActionResult Get(int num)
        {
            return Response(_manageService.GetAny(num));
        }
        /// <summary>
        /// 管理员审核结果的处理
        /// </summary>
        /// <param name="customerViewModel"></param>
        /// <returns></returns>
        [HttpPut]
        [AllowAnonymous]
        //[Authorize(Policy = "CanReviewCommodity")]
        [Route("Management")]
        public IActionResult Put([FromBody]CommodityViewModel commodityViewModel)
        {
            if (!ModelState.IsValid)
            {
                NotifyModelStateErrors();
                return Response(commodityViewModel);
            }
            _manageService.Update(commodityViewModel);

            return Response(commodityViewModel);
        }

        /// <summary>
        /// 超级管理员设置用户成为管理员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize(Policy = "CanAppointAdmin")]
        [Route("Management/Admin")]
        public IActionResult BeAdmin(string id)
        {
            _manageService.TobeAdmin(id);
            return Response();
        }
    }
}
